#!/bin/sh
#-*-sh-*-

#
# Copyright © 2009 CNRS
# Copyright © 2009-2023 Inria.  All rights reserved.
# Copyright © 2009-2012 Université Bordeaux
# Copyright © 2010-2014 Cisco Systems, Inc.  All rights reserved.
# See COPYING in top-level directory.
#

# Check the conformance of `lstopo' for all the XML
# hierarchies available here.  Return true on success.

HWLOC_top_builddir="@HWLOC_top_builddir@"
HWLOC_top_srcdir="@HWLOC_top_srcdir@"
lstopo="$HWLOC_top_builddir/utils/lstopo/lstopo-no-graphics"

HWLOC_PLUGINS_PATH=${HWLOC_top_builddir}/hwloc/.libs
export HWLOC_PLUGINS_PATH

HWLOC_DEBUG_CHECK=1
export HWLOC_DEBUG_CHECK

HWLOC_LIBXML_CLEANUP=1
export HWLOC_LIBXML_CLEANUP

if test x@HWLOC_XML_LOCALIZED@ = x1; then
  # make sure we use default numeric formats in XML outputs (non-XML outputs have to set those variables too)
  LANG=C
  LC_ALL=C
  export LANG LC_ALL
fi

error()
{
    echo $@ 2>&1
}

if [ ! -x "$lstopo" ]
then
    error "Could not find executable file \`$lstopo'."
    exit 1
fi


: ${TMPDIR=/tmp}
{
  tmp=`
    (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
  ` &&
  test -n "$tmp" && test -d "$tmp"
} || {
  tmp=$TMPDIR/foo$$-$RANDOM
  (umask 077 && mkdir "$tmp")
} || exit $?
output="$tmp/lstopo_xml.output"

set -e

testname="$1"
dirname=`dirname $1`
basename=`basename $1 .test`

if test \! -f "$testname"; then
  echo "Cannot find test file $testname"
  exit 1
fi

####################################
# foo.test should contain:
# - source: input XML filename (must be in same directory as foo.test)
# - target: expected output filename (must be in same directory as foo.test, ends with either .console or .xml)
# - options: lstopo options, if any
# - env: environment variable lines to be sourced, if any

source=`sed -n 's/^source: *//p' "$testname"`
target=`sed -n 's/^target: *//p' "$testname"`
opts=`sed -n 's/^options: *//p' "$testname"`
sed -n 's/^env: *//p' "$testname" > "$tmp/env"

if test \! -f "$dirname/$source"; then
  echo "Cannot find source file $source from test $testname"
  exit 1
fi
if test \! -f "$dirname/$target"; then
  echo "Cannot find target file $target from test $testname"
  exit 1
fi

if test "$target" = `basename $target .xml`".xml"; then
  is_xml_target=1
  opts="--of xml $opts"
else
  is_xml_target=0
  opts="--of console $opts"
fi

. "$tmp/env"

do_run()
{
  echo $lstopo --if xml --input "$dirname/$source" "$output" $opts
  $lstopo --if xml --input "$dirname/$source" "$output" $opts

  if [ "$HWLOC_UPDATE_TEST_TOPOLOGY_OUTPUT" != 1 ]
  then
    @DIFF@ @HWLOC_DIFF_U@ @HWLOC_DIFF_W@ "$dirname/$target" "$output"
  else
    if ! @DIFF@ "$dirname/$target" "$output" >/dev/null
    then
	cp -f "$output" "$dirname/$target"
	echo "Updated $target"
    fi
  fi

  if [ -n "@XMLLINT@" -a $is_xml_target -eq 1 ]
  then
    cp -f "$HWLOC_top_srcdir"/hwloc/hwloc*.dtd "$tmp/"
    ( cd $tmp ; @XMLLINT@ --valid $output ) > /dev/null
  fi

  rm "$output"
}

export HWLOC_LIBXML_IMPORT
export HWLOC_LIBXML_EXPORT

echo "Importing with default parser and reexporting with minimalistic implementation..."
HWLOC_LIBXML_IMPORT=1
HWLOC_LIBXML_EXPORT=0
do_run
echo "Importing with minimalistic parser and reexporting with default implementation..."
HWLOC_LIBXML_IMPORT=0
HWLOC_LIBXML_EXPORT=1
do_run

rm -rf "$tmp"
